#include "mprpcapplication.h"
#include <iostream>
#include <string>
#include <unistd.h>

MpRpcConfig MprpcApplication::m_config;
void ShowArgsHelp()
{
    std::cout << "format:command -i <configfile>" << std::endl;
}
MprpcApplication *MprpcApplication::GetInstance()
{
    static MprpcApplication mprpcApplication;
    return &mprpcApplication;
}
void MprpcApplication::Init(int argc, char **argv)
{
    // std::cout << "argc:" << argc << std::endl;
    if (argc < 2)
    {
        // std::cout<<11<<std::endl;
        ShowArgsHelp();
        exit(EXIT_FAILURE);
    }
    int c = 0;
    // TODO 理解
    std::string config_file;
    switch (c = getopt(argc, argv, "i:"))
    {
    case 'i':
        config_file = optarg;
        break;
    case '?':
        // std::cout << "invalid args!" << std::endl;
        ShowArgsHelp();
        exit(EXIT_FAILURE);
    case ':':
        // std::cout << "need <configfile>" << std::endl;
        ShowArgsHelp();
        exit(EXIT_FAILURE);
    default:
        break;
    }
    m_config.LoadConfigFile(config_file.c_str());

    // 开始加载配置文件 开始加载配置文件 rpcserver_ip rpcserver_port zookeeper_ip zookeeper_port
    // Config config;

    // std::cout<<"rpcserverip"<<m_config.Load("rpcserverip")<<std::endl;
    // std::cout<<"rpcserverip"<<m_config.Load("rpcserverport")<<std::endl;
    // std::cout<<"Zookeeperip"<<m_config.Load("Zookeeperip")<<std::endl;
    // std::cout<<"Zookeeperport"<<m_config.Load("Zookeeperport")<<std::endl;
}

MpRpcConfig& MprpcApplication::GetConfig()
{
    return m_config;
}